package com.ruoyi.web.controller.converter;

import java.io.*;
import java.util.*;

/**
 * @author lth
 * @PackageName:com.ruoyi.web.controller.converter
 * @ClassName:ConverterTest
 * @Description:
 * @date 2024/10/24 16:48
 */
public class ConverterTest {
    // 简单的DXF解析器（仅处理点和线）
    static class DxfParser {
        List<List<Double>> points = new ArrayList<>();
        List<List<List<Double>>> lines = new ArrayList<>();

        void parse(String content) {
            List<List<List<Double>>> lineSegments = new ArrayList<>();
            String[] stringLines = content.split("\n");
            for (String stringLine : stringLines) {
                if (stringLine.startsWith("POINT")) {
                    // ...（处理点的代码保持不变）
                } else if (stringLine.startsWith("LINE")) {
                    String[] parts = stringLine.split(",");
                    if (parts.length < 5) {
                        System.err.println("错误：LINE行格式不正确 - " + stringLine);
                        continue; // 跳过这一行，继续处理下一行
                    }
                    List<Double> start = Arrays.asList(Double.parseDouble(parts[1].trim()), Double.parseDouble(parts[2].trim()));
                    List<Double> end = Arrays.asList(Double.parseDouble(parts[3].trim()), Double.parseDouble(parts[4].trim()));
                    List<List<Double>> line = new ArrayList<>();
                    line.add(start);
                    line.add(end);
                    lineSegments.add(line);
                }
            }
            // ...（可能还有其他代码来处理lineSegments）
        }
    }

    // 将解析的数据转换为GeoJSON
    static String toGeoJson(DxfParser parser) {
        StringBuilder geoJson = new StringBuilder();
        geoJson.append("{\n");
        geoJson.append("  \"type\": \"FeatureCollection\",\n");
        geoJson.append("  \"features\": [\n");

        // 添加点
        for (int i = 0; i < parser.points.size(); i++) {
            List<Double> point = parser.points.get(i);
            geoJson.append("    {\n");
            geoJson.append("      \"type\": \"Feature\",\n");
            geoJson.append("      \"geometry\": {\n");
            geoJson.append("        \"type\": \"Point\",\n");
            geoJson.append("        \"coordinates\": ").append(Arrays.toString(point.toArray())).append("\n");
            geoJson.append("      },\n");
            geoJson.append("      \"properties\": {}\n");
            geoJson.append("    }");
            if (i < parser.points.size() - 1) {
                geoJson.append(",");
            }
            geoJson.append("\n");
        }

        // 添加线
        for (int i = 0; i < parser.lines.size(); i++) {
            List<List<Double>> line = parser.lines.get(i);
            geoJson.append("    {\n");
            geoJson.append("      \"type\": \"Feature\",\n");
            geoJson.append("      \"geometry\": {\n");
            geoJson.append("        \"type\": \"LineString\",\n");
            geoJson.append("        \"coordinates\": [");
            geoJson.append("[").append(Arrays.toString(line.get(0).toArray())).append(",");
            geoJson.append(Arrays.toString(line.get(1).toArray())).append("]");
            geoJson.append("]\n");
            geoJson.append("      },\n");
            geoJson.append("      \"properties\": {}\n");
            geoJson.append("    }");
            if (i < parser.lines.size() - 1) {
                geoJson.append(",");
            }
            geoJson.append("\n");
        }

        geoJson.append("  ]\n");
        geoJson.append("}\n");

        return geoJson.toString();
    }

    public static void main(String[] args) {
        String dxfFilePath = "G:\\Front\\dxf\\3号采掘工程平面图2000.dxf";
        String geoJsonFilePath = "G:\\Front\\dxf2geojson\\3号采掘工程平面图2000.geojson"; // 设定GeoJSON文件的保存路径

        try (BufferedReader reader = new BufferedReader(new FileReader(dxfFilePath))) {
            StringBuilder dxfContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                dxfContent.append(line).append("\n");
            }

            DxfParser parser = new DxfParser();
            parser.parse(dxfContent.toString());

            String geoJson = toGeoJson(parser); // 假设这个方法已经正确实现了

            // 将GeoJSON字符串写入文件
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(geoJsonFilePath))) {
                writer.write(geoJson);
            }

            System.out.println("GeoJSON文件已成功保存到: " + geoJsonFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("发生I/O错误: " + e.getMessage());
        }
    }
}
